var dappAddress = "n215GGCAEpbzRyLdhB5RKwShFC5DcHPK5sM";
var hash = "e113733257be5ac5caf96257fcc6fff432d4ff385b3c20aa8e8360909010be3c";
// var dappAddress = "n1kfSa8iJRCgnVw54tp4M24EBoDCsGmeWWP";
// var hash = "3cfa2005903f5b4e8c015f76fab375c0e6485b7fddf15eeb3c0314166369430f";
var NebPay = require("nebpay");
var nebPay = new NebPay();
var timer;

function toast (text) {
  $('#toast').html(text);
  $('#toast').show();
  setTimeout(function () {
    $('#toast').addClass('toast-show');
    showToast()
  }, 300);
}

function showToast () {
  setTimeout(function () {
    $('#toast').removeClass('toast-show');
    setTimeout(function () {
      $('#toast').hide();
    }, 2300);
  }, 3000);
}

function openToast (text) {
  $('#toast').html(text);
  $('#toast').show();
  setTimeout(function () {
    $('#toast').addClass('toast-show');
  }, 300);
}

function closeToast () {
  setTimeout(function () {
    $('#toast').removeClass('toast-show');
    setTimeout(function () {
      $('#toast').hide();
    }, 400);
  }, 1000); 
}

function editToast (text) {
  $('#toast').html(text);
}

function init () {
  if (typeof nebPay.simulateCall == "undefined" || typeof nebPay.call == "undefined") {
    toast('检测到你未开启星云钱包插件,请启动星云钱包插件并刷新.')

    return
  }
  get();
}

$(function () {
  init();
})

// 动画
particleground(document.getElementById('foreground'), {
  dotColor: 'rgba(59, 129, 182, 1)',
  lineColor: 'rgba(255, 255, 255, 0.06)',
  minSpeedX: 0.3,
  maxSpeedX: 0.6,
  minSpeedY: 0.3,
  maxSpeedY: 0.6,
  density: 50000,
  curvedLines: false,
  proximity: 250,
  parallaxMultiplier: 10,
  particleRadius: 4
});

// 所有输入框获取焦点以后都改变一下 border颜色
$('body').on('focus', 'input', function () {
  $(this).parent().addClass('focus')
})
$('body').on('blur', 'input', function () {
  $(this).parent().removeClass('focus')
})

// 监控添加项目那一堆输入框
// function verification () {
//   var name = $('#name').val();
//   var quota = $('#quota').val();
//   var content = $('#content').val();
//   var progress = $('#progress').val();
//   var use = $('#use').val();
//   var mailbox = $('#mailbox').val();
//   if (name && quota && content && progress && use && mailbox) {
//     $('#submit').removeClass('disabled')
//   } else {
//     $('#submit').addClass('disabled')
//   }
// }
// $('#name').on('input', function () {
//   verification()
// })
// $('#quota').on('input', function () {
//   verification()
// })
// $('#content').on('input', function () {
//   verification()
// })
// $('#progress').on('input', function () {
//   verification()
// })
// $('#use').on('input', function () {
//   verification()
// })
// $('#mailbox').on('input', function () {
//   verification()
// })


function get (name, value, time) {
  if (!name) {
    openToast('正在加载中请稍等...')
  }
  nebPay.simulateCall(dappAddress, "0", "get", JSON.stringify([]), {
    listener: function (res) {
      console.log('res');
      console.log(res);
      if(res.result == '' && res.execute_err == 'contract check failed') {
          editToast('合约检测失败，请检查浏览器钱包插件环境！');
          closeToast()
          return;
      }
      var data = JSON.parse(JSON.parse(res.result));
      console.log('data--'+data)
      
      if (name) {
        if (name === 'id') {
          for (var i = 0; i < data.length; i++) {
            if (data[i].id == value) {
              matchListHtml(data);
              clearInterval(timer);
              editToast('信息添加成功!');
              closeToast();
              $(".from").css('display','none');
              $(".community").css('display','block');
              $(".what-is").css('display','none');
            }
          }
        }

        if (name === 'time') {
          console.log('time')
          for (var i = 0; i < data.length; i++) {
            console.log(data[i].id, value, '11111')
            if (data[i].id == value) {
              for (var y = 0; y < data[i].comment.length; y++) {
                console.log(data[i].comment[y].time, time, '222222')
                if (data[i].comment[y].time == time) {
                  matchListHtml(data);
                  clearInterval(timer);
                  editToast('信息添加成功!');
                  closeToast()
                }
              }
            }
          }
        }

      } else {
        matchListHtml(data);
        $(".from").css('display','none');
        $(".community").css('display','none');
        $(".what-is").css('display','block');
        closeToast();
      }
    }
  })
}

// 提交新项目
$("#submit").click(function() { 
  var industry = $('#industry option:selected').val();
  var release_type = $('#release_type option:selected').val();
  var industry_details = $('#industry_details').val();
  var keyword = $('#keyword').val();
  var author = $('#author').val();
  var contact = $('#contact').val();
  var content = $('#content').val();
  var id = Date.now() +'_'+ ~~(Math.random() * 1e6);
  if (industry && release_type && content && industry_details && keyword && author && contact) {
    openToast('正在提交中请稍等...');
    nebPay.call(dappAddress, "0", "set", JSON.stringify([{
      id: id,
      industry: industry,
      release_type: release_type,
      industry_details: industry_details,
      keyword: keyword,
      author: author,
      contact: contact,
      content:content,
      matchObj:[]
    }]), { 
      listener: function(res){
        if (res.txhash) {
          editToast('系统正在尝试拉去信息中......请稍等 并不要操作页面');
          timer = setInterval(function () {
            get('id', id)
          }, 5000)
        } else {
          editToast('信息添加失败,请稍后再试');
          closeToast()
        }
      }
    })
  } else {
    toast('请填写完整信息');
  }
});

// 格式化时间搓的
function getTime (data) {
  var myDate = new Date(+data);
  var year = myDate.getFullYear();
  var months = myDate.getMonth() + 1;
  var month = months.toString().length === 2 ? months : '0' + months;
  var date = myDate.getDate().toString().length === 2 ? myDate.getDate() : '0' + myDate.getDate();
  var hours = myDate.getHours().toString().length === 2 ? myDate.getHours() : '0' + myDate.getHours();
  var minutes = myDate.getMinutes().toString().length === 2 ? myDate.getMinutes() : '0' + myDate.getMinutes();
  var seconds = myDate.getSeconds().toString().length === 2 ? myDate.getSeconds() : '0' + myDate.getSeconds();
  return year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds
}



function matchListHtml (data) {
  var html = '';
                
  html +='<tr class="append-html" id="append-html"><td><h3>请求帮助</h3></td><td><h3>解决方案</h3></td></tr>';   
  for (var j = 0; j < data.length; j++) {
    if (data[j].release_type=='发布请求') {
        html += '<tr><td><p>行业类型:'+data[j].industry+'</p>'
        +'<p>行业详情:'+data[j].industry_details+'</p>'
        +'<p>发布类型:'+data[j].release_type+'</p>'
        +'<p>关键字:'+data[j].keyword+'</p>'
        +'<p>发布人:'+data[j].author+'</p>'
        +'<p>联系方式:'+data[j].contact+'</p>'
        +'<p>请求详请:'+data[j].content+'</p></td>';
        if (data[j].matchObj==false) {
            html += '<td><p style="color:red">暂无匹配</p></td>';
        }else{
            html += '<td>';
            console.log('data[j].matchObj--');
            
            for (var i = 0; i < data[j].matchObj.length; i++) {
              console.log(data[j].matchObj[0].industry);
               html += '<p>行业类型:'+data[j].matchObj[i].industry+'</p>'
                +'<p>行业详情:'+data[j].matchObj[i].industry_details+'</p>'
                 +'<p>发布类型:'+data[j].matchObj[i].release_type+'</p>'
                +'<p>关键字:'+data[j].matchObj[i].keyword+'</p>'
                +'<p>发布人:'+data[j].matchObj[i].author+'</p>'
                +'<p>行业详情:'+data[j].matchObj[i].contact+'</p>'
                +'<p>方案详情:'+data[j].matchObj[i].content+'</p><br>';
            }
            html += '</td></tr>';
        }
    }
     
  }

  $('#change-html').html(html);
}